package controllers;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.node.ObjectNode;

import models.*;

import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;


import views.html.*;
import java.util.Date;



import static play.libs.Json.*;
/**
 * Created by duzi on 6/30/15.
 */
public class EnterItem extends Controller {

/*    @BodyParser.Of(BodyParser.Json.class)*/
    public  Result productInfo(Long id) {
        Product p1 = Product.find.byId(id);

        if(p1 == null){
            ObjectNode err = Json.newObject();
            err.put("code",0);
            err.put("errorMsg", "not found");
            return ok(err);
        }
        return ok(toJson(p1));
    }

    public  Result orderInfo(Long id) {
        Bill b = Bill.find.byId(id);
        b.billItemsInfo();
        return ok(toJson(b));
    }

    @BodyParser.Of(BodyParser.Json.class)
    public Result saveOrder() {
        JsonNode billJson = request().body().asJson();
        JsonNode billItems = billJson.get("items");

        Bill bill = new Bill();
        bill.billStatus = 1;
        bill.user = User.findById(Long.parseLong("10"));
        bill.customer = Customer.find.byId(billJson.get("customerId").asLong());
        if (billItems.isArray()) {
            for (JsonNode item : billItems) {
                BillItem billItem = new BillItem();
                Long date = item.get("createTime").asLong();
                billItem.billItemCreatetime = new Date(date);
                billItem.billItemSum = item.get("sum").asInt();
                billItem.billItemSumPrice = item.get("price").toString();
                billItem.bill = bill;
                billItem.product =  Product.find.where().eq("productId", item.get("productRef").asLong()).findUnique();
                billItem.billItemStatus = 1;

                if(billItem.product != null){
                    billItem.product.productStock = billItem.product.productStock - billItem.billItemSum;
                    System.out.println(billItem.product.productStock);
                    billItem.product.update();
                }

                bill.billItems.add(billItem);
            }
        }else{
            return badRequest("should be array");
        }
        bill.save();
        ObjectNode js = Json.newObject();
        js.put("id", bill.billId);
        return ok(js);
    }

    @BodyParser.Of(BodyParser.Json.class)
    public Result deleOrderItems(){
        JsonNode billJson = request().body().asJson();
        JsonNode billItems = billJson.get("items");
        if (billItems.isArray()) {
            for (JsonNode item : billItems) {
                BillItem o = BillItem.find.byId(item.asLong());
                if(o != null){
                   o.product.changeStock(o.billItemSum);
                   o.changeStatus();
                }
            }
        }else{
            return badRequest("should be array");
        }

        return ok("everything goes right!");
    }

}
